<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menus extends CI_Controller {

    public function __construct(){
        parent::__construct();

        $this->load->view('header', array('controller' => 'menus'));
    }

    public function index(){
        $this->load->model('Menu');

        $data['menus'] = $this->Menu->getList();

        $this->load->view('menus/index', $data);
    }

    public function delete($menu_id){
        $this->load->model('Menu');
        if(!empty($menu_id)){
            $this->Menu->db->delete('menus', array('menu_id' => $menu_id));
        }
        redirect('/menus');
    }

    public function add(){
        $this->load->model('Menu');

        $this->_formValidate();
        if($_POST){
            $arr = array('menu_name', 'menu_description', 'controller_id', 'action_id');
            $dataInsert = array();
            foreach($arr as $value){
                $dataInsert[$value] = $_POST[$value];
            }

            if($this->form_validation->run()){
                $res = $this->Menu->db->insert('menus', $dataInsert);
                if($res){
                    redirect('/menus');
                }
            }else{
                $data = $dataInsert;

            }
        }

        $this->load->model('Controller'); 
        $this->load->model('Action');

        $data['options_controller']  = $this->Controller->fetchPairs();
        $data['options_action']  = $this->Action->fetchPairs();

        $this->load->view('menus/add', $data); 

    }

    public function edit($menu_id){
        $this->load->model('Menu');
        $data = $this->Menu->db->get_where('menus', array('menu_id' => $menu_id), 1)->row_array();

        $this->_formValidate();
        if($_POST){

            $arr = array('menu_name', 
            'menu_description', 'controller_id', 
            'action_id');
            $dataUpdate = array();
            foreach($arr as $value){
                $dataUpdate[$value] = $_POST[$value];
            }

            if( $this->form_validation->run()){
                $res = $this->Menu->db->update('menus', $dataUpdate, array('menu_id' => $menu_id));
                if($res){
                    redirect('/menus');
                }
            }else{
                $dataUpdate['menu_id'] = $menu_id;
                $data = $dataUpdate;
            }
        }

        $this->load->model('Controller'); 
        $this->load->model('Action');

        $data['options_controller']  = $this->Controller->fetchPairs();
        $data['options_action']  = $this->Action->fetchPairs();
        $this->load->view('menus/edit', $data); 
    }

    //set form validate
    private function _formValidate(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('menu_name', 'menu name', 'required');
        $this->form_validation->set_rules('menu_description', 'menu description', 'required');
    }
}